// 引入mockjs
const Mock = require('mockjs')

const List = []
const count = 100
// ID 名称 类别 获取方式 数量 所需积分 操作人 更新时间 状态 图片
for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    timestamp: +Mock.Random.date('T'),
    operator: '@first',
    name: '@ctitle(2, 5)',
    number: '@integer(0,100)',
    point: '@integer(10,100)',
    'type|1': ['券', '实物', '平台物品'],
    'status|1':['inline', 'preparing', 'empty'],
    'getway|1': ['账户发放', '线下发放'],
    img:'@image'
  }))
}

module.exports = [
  {
    url: '/XinMiao_MS/gift/list',
    type: 'get',
    response: config => {
      const {img, status, getway, type, number,point, name, page = 1, limit = 20, sort } = config.query

      let mockList = List.filter(item => {
        if (img && item.img !== img) return false
        if (status && item.status !== status) return false
        if (getway && item.getway !== getway) return false
        if (type && item.type !== type) return false
        if (number && item.number !== number) return false
        if (point && item.point !== point) return false
        if (name && item.name.indexOf(name) < 0) return false
        return true
      })

      if (sort === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },

  {
    url: '/XinMiao_MS/gift/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      for (const gift of List) {
        if (gift.id === +id) {
          return {
            code: 20000,
            data: gift
          }
        }
      }
    }
  },

  {
    url: '/XinMiao_MS/gift/create',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },

  {
    url: '/XinMiao_MS/gift/update',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]

